package com.xysq.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class UserInfoKeeper {

    private static final String ACCOUNT_PREF = "pref_account";
    private static final String PHONE_NUMBER = "phone_number";
    private static final String REAL_NAME = "real_name";
    private static final String CUSTOMER_ID = "customer_id";
    private static final String IS_LIKE = "is_like";
    private static final String KCOUPON_ID = "kcoupon_id";
    private static final String PUBLISH_MODEL = "publish_model";
    private static final String STATES = "states";
    private static final String ORDER_ID = "orderId";
    private static final String KCUPONMODEL_ID = "kcuponModelId";
    private static final String WROD = "wrod";

    private static final String REG_PHONE = "reg_phone";
    private static final String REG_PASS = "reg_pass";
    private static final String LOGIN_PHONE = "login_phone";
    private static final String LOGIN_PASS = "login_pass";
    private static final String FIRST_ENTER = "first_enter";

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(ACCOUNT_PREF, Context.MODE_PRIVATE);
    }

    /**
     * 发布宝贝缓存
     * @param context
     * @param modelString
     */
    public  static void writePublishModel(Context context, String modelString){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(PUBLISH_MODEL, modelString);
        editor.apply();
    }
    /**
     * 读宝贝缓存
     * @param context
     * @return
     */
    public static String readPublishModel(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(PUBLISH_MODEL, "");
    }
    /**
     * 写当前用户是否喜欢改专题
     * @param context
     * @param isLike
     */
    public  static void writeIsLike(Context context, String isLike, String uuid){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(IS_LIKE + uuid, isLike);
        editor.apply();
    }

    /**
     * 读当前用户的是否喜欢专题
     * @param context
     * @return
     */
    public static String readIsLike(Context context, String uuid) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(IS_LIKE + uuid, "false");
    }

    /**
     * 写当前用户第一次进入app
     * @param context
     */
    public  static void writeFirstEnterApp(Context context){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(FIRST_ENTER, "1");
        editor.apply();
    }

    /**
     * 读当前用户是否第一次进入app
     * @param context
     * @return
     */
    public static String readFirstEnterApp(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(FIRST_ENTER, "0");
    }

    /**
     * 写当前用户的customerId
     * @param context
     * @param customerId
     */
    public  static void writeCustomerId(Context context, String customerId){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(CUSTOMER_ID, customerId);
        editor.apply();
    }
    /**
     * 写当前用户的kcouponId
     * @param context
     * @param kcouponId
     */
    public  static void writeKcouponId(Context context, String kcouponId){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(KCOUPON_ID, kcouponId);
        editor.apply();
    }

    /**
     * 读当前用户的KcouponId
     * @param context
     * @return
     */
    public static String readKcouponId(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(KCOUPON_ID, "");
    }
    /**
     * 读当前用户的customerId
     * @param context
     * @return
     */
    public static String readCustomerId(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(CUSTOMER_ID, "");
    }

    /**
     * 当前登录ID的电话号码
     *
     * @param phoneNumber phoneNumber
     */
    public static void writePhoneNumber(Context context, String phoneNumber) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(PHONE_NUMBER, phoneNumber);
        editor.apply();
    }

    /**
     * 获取当前登录ID的电话号码
     *
     * @return 返回phoneNumber
     */
    public static String readPhoneNumber(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(PHONE_NUMBER, "");
    }

    /**
     * 当前登录ID的电话号码
     *
     * @param realName realName
     */
    public static void writeRealName(Context context, String realName) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(REAL_NAME, realName);
        editor.apply();
    }

    /**
     * 获取当前登录ID的电话号码
     *
     * @return 返回phoneNumber
     */
    public static String readRealName(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(REAL_NAME, "");
    }

    /**
     * 清空 SharedPreferences 中 Token信息。
     *
     * @param context 应用程序上下文环境
     */
    public static void clear(Context context) {
        if (null == context) {
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.apply();
    }

    /**
     * 判断是否登录过
     *
     * @param context
     * @return
     */
    public static boolean judgeLogin(Context context) {
        if (!TextUtils.isEmpty(readPhoneNumber(context))) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 微信支付判断是B端还是C端支付
     * @param context
     * @param result
     */
    public  static void writeState(Context context, boolean result){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(STATES, result);
        editor.apply();
    }

    /**
     * 微信支付判断是B端还是C端支付
     * @param context
     * @return
     */
    public static boolean readStatus(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getBoolean(STATES, false);
    }
    //缓存登录时的手机号码
    public  static void writeLoginPhone(Context context, String phone){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(LOGIN_PHONE, phone);
        editor.apply();
    }
    public static String readLoginPhone(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(LOGIN_PHONE, "");
    }
    //缓存登录时的密码
    public  static void writeLoginPass(Context context, String pass){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(LOGIN_PASS, pass);
        editor.apply();
    }
    public static String readLoginPass(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(LOGIN_PASS, "");
    }
    //缓存注册时的手机号码
    public  static void writeRegPhone(Context context, String phone){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(REG_PHONE, phone);
        editor.apply();
    }
    public static String readRegPhone(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(REG_PHONE, "");
    }
    //缓存注册时的密码
    public  static void writeRegPass(Context context, String pass){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(REG_PASS, pass);
        editor.apply();
    }
    public static String readRegPass(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(REG_PASS, "");
    }


    /**
     * 缓存订单ID用以确认微信支付
     * @param context
     * @param orderId
     */
    public  static void writeOrderId(Context context, int orderId){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(ORDER_ID, orderId);
        editor.apply();
    }

    /**
     * 缓存订单ID用以确认微信支付
     * @param context
     * @return
     */
    public static int readOrderId(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getInt(ORDER_ID, 0);
    }

    /**
     * 缓存券ID用以确认微信支付
     * @param context
     * @param orderId
     */
    public  static void writeKcuponModelId(Context context, int orderId){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(KCUPONMODEL_ID, orderId);
        editor.apply();
    }

    /**
     * 缓存券ID用以确认微信支付
     * @param context
     * @return
     */
    public static int readKcuoponModelId(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getInt(KCUPONMODEL_ID, 0);
    }

    /**
     * 缓存券ID用以确认微信支付
     * @param context
     * @param word
     */
    public  static void writeWord(Context context, String word){
        if(null == context){
            return;
        }
        SharedPreferences pref = getSharedPreferences(context);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(WROD, word);
        editor.apply();
    }

    /**
     * 缓存券ID用以确认微信支付
     * @param context
     * @return
     */
    public static String readWord(Context context) {
        SharedPreferences pref = getSharedPreferences(context);
        return pref.getString(WROD,"");
    }

}
